package com.hm.oauth.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.oauth.model.AccessTokenModel;
import com.hm.oauth.repository.AccessTokenRepository;

/**
 * @author shishun.wang
 * @date 下午7:10:44 2016年8月31日
 * @version 1.0
 * @describe 
 */
@Component
public class AccessTokenService {

	@Autowired
	private AccessTokenRepository accessTokenRepository;
	
	public String put(String accessToken,String authorizeCode){
		AccessTokenModel accessTokenModel = new AccessTokenModel();
		{
			accessTokenModel.setId(accessToken);
			accessTokenModel.setAuthorizeCode(authorizeCode);
		}
		return accessTokenRepository.save(accessTokenModel).getId();
	}
	
	public String getAccssToken(String accessToken){
		AccessTokenModel accessTokenModel = accessTokenRepository.findOne(accessToken);
		if(null == accessTokenRepository){
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, accessToken);
		}
		return accessTokenModel.getAuthorizeCode();
	}
}
